package com.lx.boot1;

import com.lx.boot.oss.OssClient;
import com.lx.boot.oss.entity.DownloadUrlDTO;
import com.lx.boot.oss.entity.UploadDTO;
import com.lx.boot.oss.entity.UploadUrlDTO;
import com.lx.boot.oss.entity.UploadUrlVO;
import com.lx.boot.web.annotation.Auth;
import com.lx.boot.web.filter.DefaultApiEncryptFilter;
import com.lx.entity.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Tag(name = "Oss")
@RequestMapping("/oss")
@RestController
public class OssController {

    @PostMapping("/uploadFile")
    @Operation(summary = "上传OSS文件")
    @Auth(apiFilter = DefaultApiEncryptFilter.class)
    public Result<String> uploadFile(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {
        return Result.ok(OssClient.getInstance().upload(UploadDTO.of(file)));
    }

    @PostMapping("/getUrl")
    @Operation(summary = "获取OSS文件下载链接")
    public Result<String> getUrl(@RequestBody @Valid DownloadUrlDTO dto) throws IOException {
        String url = OssClient.getInstance().getFileUrl(dto);
        return Result.ok(url);
    }

    @Operation(summary = "获取上传文件的url 直接上传文件到oss")
    @PostMapping("/uploadUrl")
    public Result<UploadUrlVO> uploadUrl(@RequestBody @Valid UploadUrlDTO dto) throws IOException {
        return Result.ok(OssClient.getInstance().uploadUrl(dto));
    }



}
